package zjc.bookkeeping.minepage.cClassificationmanagement;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import zjc.bookkeeping.R;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.tools.ToolsClass;

public class ClassificationListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private String[] groups;
    private String[][] children;

    public ClassificationListAdapter(Context context, String[] groups, String[][] children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取组列表视图
     *
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param viewGroup
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        GroupViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_classification_group_item, null);
            holder = new GroupViewHolder();
            holder.tvTitle = convertView.findViewById(R.id.tvTitle);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText(groups[groupPosition]);

        return convertView;
    }

    /**
     * 获取子列表视图
     *
     * @param groupPosition
     * @param childPosition
     * @param isExpanded
     * @param convertView
     * @param viewGroup
     * @return
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        ChildViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_classification_item, null);
            holder = new ChildViewHolder();
            holder.tvCharacter = convertView.findViewById(R.id.text);
            holder.ivimage = convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.tvCharacter.setText(children[groupPosition][childPosition]);
        String imageName = DBManager.getConsumptionClassImage(children[groupPosition][childPosition]);
        holder.ivimage.setImageResource(ToolsClass.getResourceId(imageName));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 子视图容器
     */
    private class ChildViewHolder {
        TextView tvCharacter;
        ImageView ivimage;
    }

    /**
     * 组视图容器
     */
    private class GroupViewHolder {
        TextView tvTitle;
    }
}
